function activeLink(){
	var activeLink = $.cookie('activelink');
	if(activeLink){
		$('li').filter(".activelink").removeClass('activelink');
		$("div#navigator ul li a[href$="+"/"+activeLink.split(/http:\/\/[^\/]*\/?/)[1]+"]").parent().addClass('activelink');
	}
}
var x = "string";
$(document).ready(function() {
	//Use NitfyCute to make 'Round Corner' effective.
	//See http://www.html.it/articoli/niftycube/index.html
 	//Nifty("div#fragment_form","small transparent top bottom");
	//Nifty("ul#nav a","large transparent top");
	//Register events
	$('div#navigator ul li a').click(function(){
			var cookieName = 'activelink';
			var cookieOptions = {expires: 7, path: '/'};
			$.cookie(cookieName, this.href, cookieOptions);
	});

	/*An intersting effect */
	//$('a').click(function(){ $('body').css('backgroundColor',"black").animate({opacity:'0.5'},700).animate({opacity:'1'},500)})
	
	/*
	 * Global ajax setting
	 */
	//use http://www.contextllc.com/blog/2010/06/jquery-showloading-plugin-show-loading-graphic-over-specific-element/
	jQuery('body').ajaxStart(function(){
		$('body').showLoading();
	}).ajaxStop(function(){
		$('body').hideLoading();
	});
	
	activeLink();
	
	$("form.button_to").live(
		"ajax:success", function(event,data,status,xhr){
				var json = $.parseJSON(data);
				var selector = "#"+json[0];
				$(selector).html(json[1]);
				event.stopPropagation();
		}
	);
	$("a#vote_submit_link").live("click", function(event){
			//cache the link attributes
			$("#vote_dialog").data('href', $(this).attr('href'));
			$("#vote_dialog").data('level', $(this).attr('value'));
			
			//open a dialog to edit the vote comment.
			$("#vote_dialog").dialog(
				{
						//show: 'slide', 
						height: $.browser.msie ? 500: 340,
						width: $.browser.msie ? 420: 440,
						modal: true,
						buttons: {
							Ok : function(){
								$(this).dialog('close'); //close this dialog first.
								jQuery.post($(this).data("href"),{
									level : $(this).data("level"),
									comment: $('#vote_comment').val()
								},function(data){
									$('body').hideLoading();
									var selector = "#"+data[0];
									$(selector).html(data[1]); 
									$(selector).effect("highlight",{color:'red'},2000);
								}, "json"
								);
							}
							,
							Cancel: function() {
								$(this).dialog('close');
							}
						}
				}
			);
			event.preventDefault();
			event.stopPropagation();
			return false;
		}
	);	
	$("a#chapter_edit").live("click", function(event){
			$('form#chapter_edit').live(
				'ajax:success' , function(event, data, status, xhr){
					var obj = $.parseJSON(data);
					$('#novel_chapter_title').html(obj.chapter.title);
					$('#novel_chapter_suggest').html(obj.chapter.suggest);
					$('#chapter_body').css({backgroundColor:'red'}).fadeIn("slow",function(){
					setTimeout(function(){
						$("#chapter_body").removeAttr('style');
					}, 1000);
					});
				}
			);
			$("#chapter_form").dialog(
				{
						//show: 'slide',
						title: "Edit Chapter detail",
						height: $.browser.msie ? 500: 380,
						width: $.browser.msie ? 420: 650,
						modal: true,
						buttons: {
							Ok : function(){
								$(this).dialog('close'); //close this dialog first.
								$("form#chapter_edit").submit();
							}
							,
							Cancel: function() {
								$(this).dialog('close');
							}
						}
				}
			);
			event.preventDefault();
			event.stopPropagation();
			return false;
		}
	);

	//Add fragment to chapter
	$('form#fragment_new').live(
		'ajax:success' , function(event, data, status, xhr){
			var obj = $.parseJSON(data);
			alert(obj.fragment.content); //display new fragment
		}
	);	
	
}
);
